using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//商品
namespace ApiProject.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class RolesController : ControllerBase
    {
        private IRepository<Roles> _roleRepository;

        public RolesController(

            IRepository<Roles> roleRepository
        )
        {
            _roleRepository = roleRepository;
        }

        // 获取所有角色
        // Get /roles
        [HttpGet]
        public dynamic GetRoles()
        {
            var roles = _roleRepository.Table.Where(x => x.IsDelete == false).ToList();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = roles,
                Msg = "查找角色成功"
            });
        }


        //是否启用该角色
        // Post /roles/id
        [HttpPost("{id}")]
        public dynamic EnableRole(int id, NewRole model)
        {

            var role = _roleRepository.GetById(id);
            var isEnable = model.IsEnable;

            if (role != null)
            {
                if (isEnable == false)
                {
                    role.IsEnable = false;
                    role.UpdateTime = DateTime.Now;
                    _roleRepository.Update(role);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = role,
                        Msg = string.Format("已禁用Id为{0}的角色", id)
                    });
                }
                else
                {
                    role.IsEnable = true;
                    role.UpdateTime = DateTime.Now;
                    _roleRepository.Update(role);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = role,
                        Msg = string.Format("已启用Id为{0}的角色", id)
                    });
                }

            }
            else
            {
                return JsonHelper.Serialize(new { Code = 1000, Data = "", Msg = "禁用失败" });
            }
        }



        // 添加角色
        // Post /roles
        [HttpPost]
        public dynamic AddRoles(NewRole model)
        {
            var rolename = model.RoleName.Trim();
            var remarks = model.Remarks;

            var isRole = _roleRepository.Table.Where(x => x.RoleName == rolename).FirstOrDefault();

            if (isRole == null)
            {
                var role = new Roles
                {
                    RoleName = rolename,
                    Remarks = remarks
                };
                _roleRepository.Insert(role);

                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = role,
                    Msg = "添加角色成功"
                });

            }
            else
            {
                return JsonHelper.Serialize(new { Code = 1000, Data = "", Msg = "该角色已存在，请重新输入" });
            }
        }


        // 编辑角色
        // Put /roles/id
        [HttpPut("{id}")]
        public dynamic EditRole(int id,NewRole model){
            var isRole = _roleRepository.GetById(id);
            var rolename = model.RoleName.Trim();
            var remarks = model.Remarks;

            if(isRole != null){
                isRole.RoleName = rolename;
                isRole.Remarks = remarks;
                isRole.UpdateTime = DateTime.Now;

                _roleRepository.Update(isRole);

                return JsonHelper.Serialize(new{
                    Code = 200,
                    Data = isRole,
                    Msg = "修改成功"
                });
            }else{
                return JsonHelper.Serialize(new{
                    Code = 1000,
                    Data = "",
                    Msg = "修改失败"
                });
            }
        }




    }
}
